# coding: utf-8

import logging

from app.extensions.api import http_exceptions
from app.modules import apis
from app.modules.api.response import default
from flask_restplus_patched import Namespace, Parameters, Resource
from flask_marshmallow import base_fields

log = logging.getLogger(__name__)

api = apis[__name__]


class UpdateParam(Parameters):
    pagePath = base_fields.URL(
        description="菜单路径 例如:OMS/WarehouseManage",
        missing=None,  # default value
        required=True,
    )

    ID = base_fields.UUID(
        description="后台为这个页面元素定义的ID, 36进制, 10位长",
        missing=None,  # default value
        required=True,
    )

    componentPath = base_fields.URL(
        description="组件的引用路径",
        missing=None,
        required=True,
    )

    attr = base_fields.Dict(
        description="属性列表,{'xxx':'xxx','xxxx':'xxxxx'}",
        missing={},
        required=False,
    )

    funcs = base_fields.Dict(
        description="函数列表,{'xxx':'xxx','xxxx':'xxxxx'}",
        required=False,
        missing={},
    )


class DeleteParam(Parameters):
    pagePath = base_fields.URL(
        description="菜单路径 例如:OMS/WarehouseManage",
        missing=None,  # default value
        required=True,
    )
    ID = base_fields.UUID(
        description="后台为这个页面元素定义的ID, 36进制, 10位长",
        missing=None,  # default value
        required=True,
    )


@api.route('/update')
@api.response(
    code=http_exceptions.UnprocessableEntity.code,
    description="menu not found.",
)
class Update(Resource):
    @api.parameters(UpdateParam())
    @api.marshal_with(default(api))
    def get(self, args):

        # # 1.校验pageurl
        # validate(args)
        #
        # 2.按处理顺序管理代码文件.


        service.updatePage(page_url=args['pageUrl'],)

        return {'isSuccess': True, 'msg': 'success!'}


@api.route('/delete')
@api.response(
    code=http_exceptions.UnprocessableEntity.code,
    description="element not found.",
)
class Delete(Resource):
    @api.parameters(DeleteParam())
    @api.marshal_with(default(api))
    def get(self, args):
        return {'isSuccess': True, 'msg': 'success!'}
